package com.first.nell.components.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.databinding.DialogFirstNellPermissionAskBinding

class PermissionAskDialog : DialogFragment() {

    companion object {
        fun showDialog(
            fragmentManager: FragmentManager,
            listener: (Int) -> Unit = {},
        ) {
            val dialog = PermissionAskDialog()
            dialog.mOnActionListener = listener
            dialog.showNow(fragmentManager, PermissionAskDialog::class.java.simpleName)
        }
    }

    private lateinit var mViewBinding: DialogFirstNellPermissionAskBinding
    private var mOnActionListener: (Int) -> Unit = {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)

        mViewBinding = DialogFirstNellPermissionAskBinding.inflate(layoutInflater)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubViews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }

    private fun initSubViews() {
        mViewBinding.imageClose.setOnClickListener {
            mOnActionListener.invoke(0)
            dismiss()
        }

        mViewBinding.submitButtonCancel.setOnClickListener {
            mOnActionListener.invoke(0)
            dismiss()
        }

        mViewBinding.submitButtonConfirm.setOnClickListener {
            mOnActionListener.invoke(1)
            dismiss()
        }
    }

    /**
     * 设置点击回调
     *
     * @param Int 位置
     * @param RevObjAiHomeDemo 值
     * @param Int 类型
     */
    fun setOnActionListener(listener: (Int) -> Unit) {
        mOnActionListener = listener
    }
}